<?php
// +----------------------------------------------------------------------
// | 零云 [ 简单 高效 卓越 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.lingyun.net All rights reserved.
// +----------------------------------------------------------------------
// | Author: jry <598821125@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Model;

use Common\Model\ModelModel;

/**
 * 文章模型
 * @author jry <598821125@qq.com>
 */
class ContractModel extends ModelModel
{
    public $budget = array('1' => '50万~100万','2' => '100万~200万','3'=>'200万以上');
    public $cootype = array('1' => '产品体验(基础植入/圈层营销/综合课堂)','2' => '主题传播(微整合营销/定制策划)','3'=>'口碑转化(年度代言/KOL合作)');
    public $target = array('1' => '品牌理念深度传播','2' => '新品上市','3'=>'VIP会员活动');
    /**
     * 数据库真实表名
     * 一般为了数据库的整洁，同时又不影响Model和Controller的名称
     * 我们约定每个模块的数据表都加上相同的前缀，比如微信模块用weixin作为数据表前缀
     * @author jry <598821125@qq.com>
     */
    protected $tableName = 'admin_contract';

    /**
     * 自动验证规则
     * @author jry <598821125@qq.com>
     */
    protected $_validate = array(
        array('brand_id', 'require', '品牌方不得为空', self::MUST_VALIDATE, 'regex', self::MODEL_INSERT),
        array('product', 'require', '植入产品不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('product_info_txt', 'require', '产品信息不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('target', 'require', '植入目地不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        //array('title', '', '标题已经存在', self::VALUE_VALIDATE, 'unique', self::MODEL_BOTH),
    );

    /**
     * 自动完成规则
     * @author jry <598821125@qq.com>
     */
    protected $_auto = array(
        array('create_time', 'time', self::MODEL_INSERT, 'function'),
        array('update_time', 'time', self::MODEL_BOTH, 'function'),
        array('status', '1', self::MODEL_INSERT),
    );

    /**
     * 查找后置操作
     * @author jry <598821125@qq.com>
     */
    protected function _after_find(&$result, $options)
    {
        

         $result['product_info_upload_url'] = get_cover($result['product_info_upload'], 'default');
         
         //$result['product_info_upload_url'] = $this->multi_upload($result['product_info_upload']);
         $result['brand_user'] = D('Admin/User')->getUserInfo($result['brand_id'],'nickname');
         
         $temp = explode(',',$result['budget']);
         foreach ($temp as $value) {
            $t_data .= $this->target[$value]."<br>";
         }
         
         $result['budget_txt'] = $t_data;
         unset($t_data);

         $temp = explode(',',$result['cootype']);
         foreach ($temp as $value) {
            $t_data .= $this->target[$value]."<br>";
         }
         
         $result['cootype_txt'] = $t_data;
         unset($t_data);

         $temp = explode(',',$result['target']);
         foreach ($temp as $value) {
            $t_data .= $this->target[$value]."<br>";
         }
         
         $result['target_txt'] = $t_data;
         unset($t_data);
         

    }

    private function multi_upload($ids) {
        $temp = explode(',', $ids);
        foreach ($temp as $value) {
            $t_upload = D('Admin/Upload')->getUploadInfo($value);
            $url = U(MODULE_NAME . '/Upload/download',array('token' => $t_upload['download_token']));
            $data.= "<a href='".$url."'>".$t_upload["name"]."</a><br>";
            //$data[] = D('Admin/Upload')->getUploadInfo($value);    
        }
        return $data;
    }

    private function get_member($ids) {
        $temp = explode(',', $ids);
        foreach ($temp as $value) {
            $data[] = D('Admin/User')->getUserInfo($value,'nickname');    
        }
        
        return $data;
    }

    /**
     * 查找后置操作
     * @author jry <598821125@qq.com>
     */
    protected function _after_select(&$result, $options)
    {
        foreach ($result as &$record) {
            $this->_after_find($record, $options);
        }
    }

    /**
     * 获取列表
     * @author jry <598821125@qq.com>
     */
    public function getList($cid, $limit = 10, $page = 1, $order = null, $map = null)
    {
        $con["status"] = array("eq", '1');
        if ($map) {
            $map = array_merge($con, $map);
        }
        if (!$order) {
            $order = 'id desc';
        }
        $list = $this->page($page, $limit)
            ->order($order)
            ->where($map)
            ->select();

        return $list;
    }
}
